// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.mapper.InventoryListDMapper;
import com.fowo.api.model.inventory.list.InventoryListGetInventoryParam;
import com.fowo.api.model.inventory.list.InventoryListGetInventoryResult;
import com.fowo.api.model.inventory.list.InventoryListGetProductParam;
import com.fowo.api.model.inventory.list.InventoryListGetProductResult;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import com.fowo.api.user.model.JwtUserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 盘点单 服务实现类
 */
@Service
public class InventoryListServiceImpl extends InventoryListBaseServiceImpl {

    @Resource
    private InventoryListDMapper mapper;

    public List<InventoryListGetInventoryResult> getInventoryList(InventoryListGetInventoryParam param) {
        return mapper.getInventory(param);
    }

    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了作废操作"
    )
    @Override
    public void voidInventory(List<Long> ids) throws Exception {
        for(Long id : ids){
            inventoryListCancellationAction.run(id);
        }
    }

    public Page<InventoryListGetProductResult> getProduct(InventoryListGetProductParam param) {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        if (currentUser != null) {
            param.setCurrentUserId(currentUser.getUserId());
        }
//        param.setDisableDataPremissions(true);
        return mapper.selectProduct(param.toPage(), param);
    }

    public List<InventoryListGetInventoryResult> getInventoryByBins(String bins,Boolean zeroParticipation){
        if(StringUtils.isBlank(bins)){
            return new ArrayList<InventoryListGetInventoryResult>();
        }
        final String[] split = bins.split(",");
        List<Long> binList = new ArrayList<>();
        for(String binStr : split){
            binList.add(Long.valueOf(binStr));
        }
        return mapper.getInventoryByBins(binList,zeroParticipation);
    }
}
